home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / ibm / sym_r3.arc / SIM_TIME.C < prev    next >
C/C++ Source or Header  |  1989-07-08  |  2KB  |  90 lines

  1. /************************
  2.     Program: 68705R3 simulator
  3.     File: sim_time.c 
  4.     initialize and simulate timer tics
  5. ************************/
  6. #include "sim.h"
  7.  
  8. /* #define DEBUG */   /* uncomment to see timer count on prompt line */
  9.  
  10. static int internal_count; /* counts up to prescaler value */
  11. static int prescale;
  12. static int tcr_copy;
  13. static int previous_timer_ext_in;
  14.  
  15. sim_timer_init()
  16. /* Called from sim_reset */
  17. {
  18.     int mor;
  19.     
  20.     mor = sim_readf(MASK_OPT_REG);
  21.     internal_count = 0;
  22.     sim_write( TIMER_DATA, 0xFF);
  23.  
  24.     tcr_copy = 0x40 | (mor & 0x37);
  25.  
  26.     if (mor & 0x40)     /* TOPT bit (bit 6 in MOR) is set */
  27.     tcr_copy |= 0x10;   /* TIE (Timer external input enable) */
  28.  
  29.     sim_write( TIMER_CONTROL, tcr_copy );  /* Causes sim_timer_ctrl call */
  30. }
  31.  
  32. sim_timer_ctrl()
  33. /* Called when TCR is written to */
  34. {
  35.     tcr_copy = sim_readf( TIMER_CONTROL);
  36.     if (tcr_copy & 0x08)     /* Prescaler clear */
  37.     internal_count = 0;
  38.     prescale = 1 << (tcr_copy & 0x07);
  39. #ifdef DEBUG
  40.     printf( "Prescale = %d", prescale);
  41. #endif
  42. }
  43.  
  44. sim_timer_update( ticks)
  45. int ticks;
  46. {
  47.     int tmode, temp, td;
  48.  
  49.     tmode = tcr_copy & 0x30;  /* TIN and TIE */
  50.     if (tmode == 0x00)
  51.     temp = ticks;
  52.     else if (tmode == 0x10)
  53.     {
  54.     if (timer_external_input)
  55.         temp = ticks;
  56.     else
  57.         temp = 0;
  58.     }
  59.     else if (tmode == 0x20)
  60.     temp = 0;
  61.     else if (tmode == 0x30)
  62.     {
  63.     if (timer_external_input && !previous_timer_ext_in)
  64.         temp = 1;
  65.     else
  66.         temp = 0;
  67.     }
  68.     previous_timer_ext_in = timer_external_input; /* save for next time */
  69.  
  70.     internal_count += temp;
  71.  
  72.     /* Prescale should never be 0, but test to prevent hang */
  73.     while ((internal_count >= prescale) && (prescale != 0))
  74.     {
  75.     internal_count -= prescale;
  76.     td = sim_readf( TIMER_DATA);
  77.     td--;
  78.     if (td == 0)
  79.         sim_write(TIMER_CONTROL, tcr_copy | 0x80); /* Timer Interrupt Req */
  80.         /* Note: sim_write causes sim_timer_ctrl to be called */
  81.     if (td < 0)
  82.         td = 0xFF;
  83.     sim_write( TIMER_DATA, td);
  84.     }
  85. #ifdef DEBUG
  86.     td = sim_readf( TIMER_DATA);
  87.     printf( "Internal count = %d, td = %d    ", internal_count, td);
  88. #endif
  89. }
  90.